﻿using FTBS.Models.BLO;
using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FTBS.Controllers
{
    public class SupplierController : Controller
    {
        private UserBLO _userBLO = new UserBLO();

        /// <summary>
        /// Display all suppliers
        /// </summary>
        /// <returns>list of suppliers</returns>
        /// <author>hungph</author>
        public ActionResult Index()
        {
            return View(_userBLO.DisplayAllSuppliers());
        }

        /// <summary>
        /// Render view Create
        /// </summary>
        /// <returns></returns>
        /// <author>hungph</author>
        public ActionResult Create()
        {
            ViewBag.UserName = null;
            ViewBag.Email = null;
            return View();
        }

        /// <summary>
        /// create a supplier
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        [HttpPost]
        public ActionResult Create(User user, HttpPostedFileBase file)
        {
            if (_userBLO.CheckUsername(user))
            {
                ViewBag.UserName = user.UserName;
            }
            if (_userBLO.CheckEmail(user))
            {
                ViewBag.Email = user.Email;
            }
            if (ModelState.IsValid)
            {
                if (!_userBLO.CheckUsername(user) && !_userBLO.CheckEmail(user))
                {
                    if (file != null)
                    {
                        file.SaveAs(HttpContext.Server.MapPath("~/SupplierImages/") + file.FileName);
                        user.ImageURL = file.FileName;
                    }
                    ViewBag.UserName = null;
                    ViewBag.Email = null;
                    var isSuccess = _userBLO.CreateSupplier(user);
                    if (isSuccess)
                    {
                        return RedirectToAction("Index");
                    }
                    return View(user);
                }
            }
            return View(user);
        }

        /// <summary>
        /// load supplier and render view edit info of supplier
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        public ActionResult Edit(String id)
        {
            User user = _userBLO.LoadUserByUserName(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            this.Session["User"] = user.UserName;
            return View(user);

        }

        /// <summary>
        /// edit info of supplier
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        [HttpPost]
        public ActionResult Edit(User user)
        {
            var email = _userBLO.LoadUserByUserName(this.Session["User"].ToString().Trim()).Email;
            if (email == user.Email)
            {
                ViewBag.Email = null;
            }
            if (email != user.Email)
            {
                ViewBag.Email = user.Email;
            }
            if (ModelState.IsValid)
            {
                var isExist = _userBLO.CheckEmail(user);
                if (!isExist || (ViewBag.Email == null))
                {
                    var isSuccess = _userBLO.EditSupplier(user);
                    if (isSuccess)
                    {
                        this.Session["User"] = null;
                        return RedirectToAction("Index");
                    }
                }
            }
            return View(user);
        }

        /// <summary>
        /// active supplier
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        public ActionResult Active(String id)
        {
            User user = _userBLO.LoadUserByUserName(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            bool isActive = _userBLO.ActiveUser(user);
            return RedirectToAction("Index");
        }

        /// <summary>
        /// deactive user
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        public ActionResult Deactive(String id)
        {
            User user = _userBLO.LoadUserByUserName(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            bool isActive = _userBLO.DeactiveUser(user);
            return RedirectToAction("Index");
        }

        /// <summary>
        /// Delete user
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Redirect to Index page</returns>
        /// <author>hungph</author>
        [HttpDelete, ActionName("Delete")]
        public ActionResult Delete(String id)
        {
            User user = _userBLO.LoadUserByUserName(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            _userBLO.DeleteUser(user);
            return RedirectToAction("Index");
        }
    }
}
